function toggleTinyMCE(editor_id)
{
    if (window[editor_id + "_editor"]){tinymce.execCommand('mceRemoveControl',false,editor_id);}
    else {tinymce.execCommand('mceAddControl',false,editor_id);}
    window[editor_id + "_editor"] = !window[editor_id + "_editor"]
}
$(document).ready(function(){
    if (window.location != window.parent.location) //Check that we're in an iframe
    {
        //For every iframe needing resizing...
        $('iframe.resize_needed',window.parent.document).each(function(index) {
            var visible = $(this).is(':visible');
            //Make the parent expansion container visible if it exists and the child is hidden
            if (!visible){$(this).parent('.expansion_content').css({'position':'absolute','visibility':'hidden','display':'block'});}
            //alert('body ' + $(document).find('body').height() + ' html ' + $(document).find('html').height() + ' document ' + $(document).height());
            //Get the new height (from the body tag) - this is the most appropriate found during testing but it is always a little short
            var new_height = $(document).find('body').height()+ 25;
            //Re-hide the expansion container if necessary
            if (!visible){$(this).parent('.expansion_content').css({'position':'static','visibility':'visible','display':'none'});}
            //Set our new height
            $(this).height(new_height);
            //If we shrunk the div, scroll it back into view
            //if (new_height - $(this).height() < 0)
            //{
                $(this)[0].scrollIntoView(true);
            //}
        });
    }
    
    setTimeout('$("#messages").hide();',10000);
    $(".expansion_title").click(function()
        {
            //Bug in Chrome 24 - screen will flash when scrollbar disappears (screen may disappear altogether)
            $(this).siblings(".expansion_content").slideToggle();
        });
    //$(".expansion_title").click(function(){if ($(this).siblings(".expansion_content").is(":visible")){$(this).siblings(".expansion_content").hide();}else{$(this).siblings(".expansion_content").slideDown();}});
    $(".expansion_footer").click(function(){if ($(this).parent(".expansion_content").is(":visible")){$(this).parent(".expansion_content").slideUp();}else{$(this).parent(".expansion_content").slideDown();}});
    $(".expansion_title").attr('title','Click to view. Click again to collapse.');
    $(".expansion_footer").attr('title','Click to collapse this section.');
    $(".expansion_content:not(.expansion_open)").hide();
    if (typeof setup_tinymce == 'function')
    {
        $("textarea").not('.mceNoEditor').not('.mceNoToggle').each(function(index){
            window[$(this).attr('id') + "_editor"] = !$(this).hasClass('mceEditorOff');
            $(this).before("<button type='button' class='button smallbutton' onclick=\"toggleTinyMCE('" + $(this).attr('id') + "');\">Toggle Editor</button><br>");
        });
    }
    var hash = location.hash.replace('#','');
    if(hash != ''){
        $(".expansion_title").each(function(){
            if ($(this).find('a').size() > 0)
            {
                if ($(this).find("a").prop('name') == hash){$(this).siblings(".expansion_content").show();}
            }
        });
    }
});